#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<iostream>
#include<stdio.h>
#include<unistd.h>
#include<netdb.h>
#include<errno.h>
#include <string.h>
#include <signal.h>

#define SIZE 409600
#define MAX_CLIENTS 510
#define PORT 31289
using namespace  std;

struct message {
	char * msg_;
	char * command_;
	message(char* command, char* msg, bool zipped) : msg_(msg), command_(command){}
	char * get_message() {return msg_;}//TODO return concat of msg && command(check if needed)
}
struct client {
	int client_sckt;
	int server_sckt;
	sockaddr_in server_info;
	sockaddr_in client_info;
	int cur_version; 
	void do_server_search();
	void connect_to_server();
	void send_message(message msg);
	message receive_message();
	void disconnect_from_server();
} 

void client :: do_server_search() {
	int bcast_sckt = socket(AF_INET, SOCK_DGRAM, 0);
	int t = 1;
	setsockopt(broadcast_sock, SOL_SOCKET, SO_BROADCAST, &t, sizeof(t));
	
	sockaddr_in* s_host = new sockaddr_in();
	s_host->sin_family = AF_INET;
	s_host->sin_port = htons(PORT);
	s_host->sin_addr.s_addr = htonl(INADDR_BROADCAST);

	char[128] msg = {};
	int sockaddr_len = sizeof(sockaddr_in);
	sendto(bcast_sckt, msg, sizeof(msg), 0, (sockaddr_t*)s_host, sockaddr_len);
	recvfrom(client.broadcast_sock, buf, sizeof(buf), 0, (sockaddr_t*)&server_info, &sockadd_len));
}
//what to do if there are a lot of servers in the network?
void client :: connect_to_server() {
	client_sckt = socket(AF_INET, SOCK_STREAM, 0);
	connect(client_sckt, server_info, sizeof(server_info));
}

void client :: send_message(message msg) {
	char * msg = msg.get_message();//todo send_all
	send(client_sckt, msg, sizeof(msg), 0);
}
message receive_message();
void disconnect_from_server() {
	message msg("BYE","", false`);
	send_message(msg);
}



int main(int argc, char** argv)
{
	int errcheck = 0;
	int size = 0;
   	char recv_buf[SIZE];
	char buf[SIZE];
	char hname[256];
	char req[500];
	
	fd_set all_connections;
	FD_ZERO(&all_connections);	



 
	int max_num = bcast_sckt;
	
	while(true) {
		if(!FD_ISSET(bcast_sckt,&all_connections)) {
				FD_SET(bcast_sckt,&all_connections);                                                  
		}
		
		if(!FD_ISSET(0,&all_connections)) {	
			FD_SET(0,&all_connections);
		}
		
		errcheck = select(max_num, &all_connections, 0, 0, 0);
		if(errcheck <= 0) {
			perror("select: ");
			close(remote_server_sckt);
			return -1;
		}
		
		if(FD_ISSET(bcast_sckt, &all_connections)) {\
			
			size = recv(remote_server_sckt,buf + end,SIZE-end,0);
			if(size <= 0) {
				if(size < 0)
					perror("Receive: ");
				FD_CLR(remote_server_sckt, &all_connections);
				close(remote_server_sckt);
				flag = true;
				continue;
			}
			end += size;	
		}
		if(FD_ISSET(0,&all_connections))
		{
		counter = 25;
		while(begin != end && counter)
		{     
			if(buf[begin] == '\n')
			{
			counter --;		
			if(!counter)
				cout << "Press space to scroll down " << endl;	
			}
			printf("%c",buf[begin]);
			begin++;
		
		}
		if(begin == end && flag)
				break;
		}
	}
return 0;
}
